//注意:实际运行的是preload.js文件
//console.log('preload.ts') 
const path = require('path')
const {
  contextBridge,
  ipcRenderer
} = require('electron')

//官网建议使用contextBridge方式
contextBridge.exposeInMainWorld('electronAPI', {

  // 渲染器调用主进程返回数据  （类似ajax）（请求-处理-等待返回值）官方建议
  electronAjax: (args) => ipcRenderer.invoke('electronAjax', args),

  // 渲染器进程到主进程（2个单向组合成一个双向）
  send: (channel, data) => {
    // console.log('preload-send', channel)
    // whitelist channels
    const validChannels = ['electronRenderToMain']
    if (validChannels.includes(channel)) {
      ipcRenderer.send(channel, data)
    }
  },

  // 主进程到渲染器进程
  receive: (channel, callback) => {
    //console.log('preload-receive', channel)
    // whitelist channels
    const validChannels = ['electronMaintoRender']
    if (validChannels.includes(channel)) {
      ipcRenderer.on(channel, callback)
    }
  },

  removeListener: (channel, listener) => {
    ipcRenderer.removeListener(channel, listener);
  },

  removeAllListeners: (channel) => {
    ipcRenderer.removeAllListeners(channel);
  }

})


